//
//  HHLoginWithPhoneViewController.m
//  HHBroker
//
//  Created by He Wei on 9/22/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHLoginWithPhoneViewController.h"
#import "HHLoginWithAuthCodeViewController.h"
#import "NSString+ValidateArgument.h"
#import "HttpRequestManager.h"
#import "HHWelcomeViewController.h"
#import "HHLoginWithPhoneNavi.h"



@interface HHLoginWithPhoneViewController ()

@property (nonatomic, weak) IBOutlet UITextField *phoneNumberTextField;
@property (nonatomic, weak) IBOutlet UIButton *getAuthCodeButton;

@end

@implementation HHLoginWithPhoneViewController
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"短信验证码登录";
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    UIBarButtonItem  *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonClicked:)];
    self.navigationItem.rightBarButtonItem =cancelButton;
}

#pragma mark - Delegate
#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101) {
        if (buttonIndex == 1) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            HHLoginWithPhoneNavi *rootVC = (HHLoginWithPhoneNavi *)self.navigationController;
            if ([rootVC.delegate respondsToSelector:@selector(loginWithPhoneViewController:didCancelWithResult:)]) {
                [rootVC.delegate loginWithPhoneViewController:rootVC didCancelWithResult:LoginCancelBySignedPhone];
            }
        }
        return;
    }
    if (buttonIndex == 1) {
        __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hub.mode = MBProgressHUDModeIndeterminate;
        //网络请求
        //加入加密字段
        NSString *encPhone = self.phoneNumberTextField.text;
        NSString *urlString = [NSString stringWithFormat:@"%@&username=%@", KUrlLoginGetCode, encPhone];
        
        HHLoginWithAuthCodeViewController *loginWithCode = [[HHLoginWithAuthCodeViewController alloc] init];
        loginWithCode.readAuthorCodePhone = self.phoneNumberTextField.text;
        [self.navigationController pushViewController:loginWithCode animated:YES];
        return;

        [HttpRequestManager getWithUrlString:urlString success:^(id response){
            NSInteger code = [[response objectForKey:@"code"] integerValue];
            if (code == 1) {
                //send success
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
                hub.mode = MBProgressHUDModeCustomView;
                hub.customView = imageView;
                hub.labelText = NSLocalizedString(@"发送成功，请查看手机短信", nil);;
                hub.completionBlock = ^ {
                    HHLoginWithAuthCodeViewController *loginWithCode = [[HHLoginWithAuthCodeViewController alloc] init];
                    loginWithCode.readAuthorCodePhone = self.phoneNumberTextField.text;
                    [self.navigationController pushViewController:loginWithCode animated:YES];
                };
                [hub hide:YES afterDelay:1];
            } else {
                NSString *msgString = [response objectForKey:@"msg"];
                if (code == -3) {
                    [hub hide:NO];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"该手机号尚未注册，可使用该手机号注册", nil) message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:@"注册", nil];
                    [alertView  show];
                    alertView.tag = 101;
                } else {
                    hub.mode = MBProgressHUDModeText;
                    hub.labelText = msgString;
                    [hub hide:YES afterDelay:2];
                }
            
            }
        
        } failure:^(NSError *error) {
            hub.labelText = @"网络有点问题";
            [hub hide:YES afterDelay:2];
        
        } isLogin:NO];
    }
}

#pragma mark - Event Response

- (IBAction)textFieldValueChanged:(UITextField *)textField
{
    if (textField == self.phoneNumberTextField) {
        self.getAuthCodeButton.enabled = textField.text.length > 0 ? YES : NO;
    }
}

- (IBAction)getAuthCodeButtonClicked:(UIButton *)sender
{
    // check
    if (![NSString validateMobile:self.phoneNumberTextField.text]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    NSString *encPhone = self.phoneNumberTextField.text;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认手机号码" message:[NSString stringWithFormat:@"我们将发送短信验证码到这个号码:%@",encPhone] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    [alert show];
}

- (void)cancelButtonClicked:(UIBarButtonItem *)item
{
    SEL selector =
    NSSelectorFromString(@"loginWithPhoneViewController:didCancelWithResult:");
    if ([self.navigationController.delegate respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.navigationController.delegate performSelector:selector withObject:self.navigationController];
#pragma clang diaagnostic pop
    }

}

@end
